package Utils;

import Pojo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.*;

public class DBUtil {
    private Logger logger = Logger.getLogger(DBUtil.class);

    public static void main(String[] args) {

        DBUtil dbUtil = new DBUtil();
        String sql = "select * from `user` where userTelPhone = ?";
        Object[] params = {"13768093256"};
        try {
            System.out.println(dbUtil.queryData(sql, params, User.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 对数据库进行增删改操作
     *
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public int update(String sql, Object[] params) throws SQLException {
        QueryRunner query = new QueryRunner(DruidUtil.getDataSource());
        logger.info("update=>sql:" + sql);
        logger.info("update=>object[]:" + Arrays.toString(params));
        return query.update(sql, params);
    }

    /**
     * 查询count类型的参数
     *
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public int queryCount(String sql, Object[] params) throws SQLException {
        QueryRunner query = new QueryRunner(DruidUtil.getDataSource());
        logger.info("queryCount=>sql:" + sql);
        logger.info("queryCount=>object[]:" + Arrays.toString(params));
        return Integer.parseInt(query.query(sql, new ScalarHandler(), params).toString());
    }

    /**
     * 查询数据库内容返回指定类型的实体类
     *
     * @param sql
     * @param params
     * @param T
     * @param <T>
     * @return
     * @throws SQLException
     */
    public <T> T queryData(String sql, Object[] params, Class<? extends T> T) throws SQLException {
        QueryRunner query = new QueryRunner(DruidUtil.getDataSource());
        logger.info("queryData=>sql:" + sql);
        logger.info("queryData=>object[]:" + Arrays.toString(params));
        return query.query(sql, new BeanHandler<T>(T), params);
    }

    /**
     * 返回一个List集合类型的实体数据
     *
     * @param sql
     * @param params
     * @param T
     * @param <T>
     * @return
     * @throws SQLException
     */
    public <T> List<T> queryDatas(String sql, Object[] params, Class<? extends T> T) throws SQLException {
        QueryRunner query = new QueryRunner(DruidUtil.getDataSource());
        logger.info("queryDatas=>sql:" + sql);
        logger.info("queryDatas=>object[]:" + Arrays.toString(params));
        return query.query(sql, new BeanListHandler<T>(T), params);
    }

    /**
     * 以map的形式返回自定义数据
     *
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public Map<String, Object> queryCustom(String sql, Object[] params) throws SQLException {
        QueryRunner query = new QueryRunner(DruidUtil.getDataSource());
        logger.info("queryCustom=>sql:" + sql);
        logger.info("queryCustom=>object[]:" + Arrays.toString(params));
        return query.query(sql, new MapHandler(), params);
    }

    /**
     * 以Map集合的形式返回自定义数据
     *
     * @param sql
     * @param params
     * @return
     */
    public List<Map<String, Object>> queryCustoms(String sql, Object[] params) throws SQLException {
        QueryRunner query = new QueryRunner(DruidUtil.getDataSource());
        logger.info("queryCustoms=>sql:" + sql);
        logger.info("queryCustoms=>object[]:" + Arrays.toString(params));
        return query.query(sql, new MapListHandler(), params);
    }

}
